package com.senior.completable;


import lombok.Getter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;


/**
 * 需求，在各大网站，找到同一本书，找出最低价
 */
public class CompletableFutureNetMallDemo {
    static List<NetMall> list = Arrays.asList(
            new NetMall("jd"),
            new NetMall("pdd"),
            new NetMall("tb")
    );

    //同步
    public static List<String> getPriceByStep(List<NetMall> list,String productName){
       //这是stream写法
//        return list
//               .stream() //String.format 就是把拼成字符串
//               .map(netMall -> String.format(productName+" in %s price is %.2f %s",netMall.getMallName(),netMall.calcPrice(productName)))
//               .collect(Collectors.toList());

        //这是正常写法
        List<String> list1 = new ArrayList<>();
        for (NetMall netMall : list) {
            list1.add(String.format(productName+" in %s price is %.2f",netMall.getMallName(),netMall.calcPrice(productName)));
        }
        return list1;
    }
    //异步
    public static List<String> getPriceByAsYnc(List<NetMall> list,String productName){
        //这是stream写法  其实就是在for里面开启线程，有多少个商城开几个线程，最后在统计到list中
        return list.stream()
                 .map(netMall -> CompletableFuture.supplyAsync(
                         ()->String.format(productName+" in %s price is %.2f",netMall.getMallName(),netMall.calcPrice(productName))
                 ))
                 .collect(Collectors.toList())
                 .stream()
                 .map(CompletableFuture::join)
                 .collect(Collectors.toList());

         //下面是正常写法
//        List<String> list1 = new ArrayList<>();
//        List<CompletableFuture<String >> futureList = new ArrayList<>();
//        for (NetMall netMall : list) {
//            CompletableFuture<String> stringCompletableFuture = CompletableFuture.supplyAsync(() ->
//                    String.format(productName + " in %s price is %.2f", netMall.getMallName(), netMall.calcPrice(productName)));
//            futureList.add(stringCompletableFuture);
//        }
//        for (CompletableFuture completableFuture : futureList) {
//            list1.add((String) completableFuture.join());
//        }
//
//        return list1;

    }


    public static void main(String[] args) throws InterruptedException {
        long start = System.currentTimeMillis();
        List<String> mysql = getPriceByStep(list, "mysql");
        for (String s : mysql) {
            System.out.println(s);
        }
        long end = System.currentTimeMillis();
        System.out.println(end - start + "毫秒");

        System.out.println("------------------------");

        long start1 = System.currentTimeMillis();
        List<String> mysql1 = getPriceByAsYnc(list, "mysql");
        for (String s : mysql1) {
            System.out.println(s);
        }
        long end1 = System.currentTimeMillis();
        System.out.println(end1 - start1 + "毫秒");
    }

}

//商城类
class NetMall{

    @Getter
    private String mallName;//商场名称

    public NetMall(String mallName){
        this.mallName = mallName;
    }


    //产品的随机价格
    public double calcPrice(String productName){
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //就是创建一个随机价格
        return ThreadLocalRandom.current().nextDouble() * 2 +productName.charAt(0);
    }

}